using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace EmailService
{
	public class EmailService : System.ServiceProcess.ServiceBase
	{
		private EventLog elEvents = new EventLog();
		private double interval = 600000;			
		private System.Configuration.AppSettingsReader configurationAppSettings;

		internal System.Timers.Timer Timer1;
		/// <summary> 
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public EmailService()
		{
			// 该调用是 Windows.Forms 组件设计器所必需的。
			InitializeComponent();

			// TODO: 在 InitComponent 调用后添加任何初始化
		}

		// 进程的主入口点
		static void Main()
		{
			System.ServiceProcess.ServiceBase[] ServicesToRun;
	
			// 同一进程中可以运行多个用户服务。若要将
			//另一个服务添加到此进程，请更改下行
			// 以创建另一个服务对象。例如，
			//
			//   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
			//
			ServicesToRun = new System.ServiceProcess.ServiceBase[] { new EmailService() };

			System.ServiceProcess.ServiceBase.Run(ServicesToRun);
		}

		/// <summary> 
		/// 设计器支持所需的方法 - 不要使用代码编辑器 
		/// 修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.Timer1 = new System.Timers.Timer();
			((System.ComponentModel.ISupportInitialize)(this.Timer1)).BeginInit();
			// 
			// Timer1
			// 
			this.Timer1.Enabled = true;
			this.Timer1.Interval = 60000;
			this.Timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.Timer1_Elapsed);
			// 
			// EmailService
			// 
			this.ServiceName = "SOW EmailService";
			((System.ComponentModel.ISupportInitialize)(this.Timer1)).EndInit();

		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// 设置具体的操作，以便服务可以执行它的工作。
		/// </summary>
		protected override void OnStart(string[] args)
		{
			configurationAppSettings = new System.Configuration.AppSettingsReader();

			try
			{
				string TimeInterval = (string)configurationAppSettings.GetValue("Interval", Type.GetType("System.String"));
				interval = Convert.ToDouble(TimeInterval) * 1000;
			}
			catch
			{
				interval = 600000;
			}

			elEvents.Source = "SOW Email Service";
			elEvents.WriteEntry("Starting Email Send Service on thread:" +  AppDomain.GetCurrentThreadId().ToString(), EventLogEntryType.Information);
			EmailBS.WriteEmailLog(DateTime.Now.ToString() + ":Starting Email Service");
			this.Timer1.Enabled = true;
			this.Timer1.Interval = interval;
		}
 
		/// <summary>
		/// 停止此服务。
		/// </summary>
		protected override void OnStop()
		{
			// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
		}

		private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			this.Timer1.Enabled = false;

			try
			{
//				elEvents.WriteEntry("开始执行，时间：" + DateTime.Now.ToString() + "\n");
				EmailBS.SendEmail();
			}
			catch(Exception ex)
			{
				EmailBS.WriteEmailLog(DateTime.Now.ToString() + ":错误发生：" + ex.ToString());
//				elEvents.WriteEntry("执行错误，时间：" + DateTime.Now.ToString() + "\n" + ex.ToString(), EventLogEntryType.Error);
			}

			try
			{
				string TimeInterval = (string)configurationAppSettings.GetValue("Interval", Type.GetType("System.String"));
				Timer1.Interval = Convert.ToDouble(TimeInterval) * 1000;
			}
			catch
			{}

			this.Timer1.Enabled = true;
		}
	}
}
